package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.commons.SystemInfo;
import com.suineg.pms.service.commoms.SystemInfoService;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.component.application.report.configbean.editablereport.EditableReportInsertDataBean;
import com.wabacus.system.component.application.report.configbean.editablereport.EditableReportUpdateDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 系统参数 拦截器
 *@author Suine
 */
public class SystemInfoIntercepter extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		
		Long pid = Long.valueOf(mRowData.get(Messages.getString("SystemInfoIntercepter.0"))); //$NON-NLS-1$
		String code = mRowData.get(Messages.getString("SystemInfoIntercepter.1")); //$NON-NLS-1$
		String name = mRowData.get(Messages.getString("SystemInfoIntercepter.2")); //$NON-NLS-1$
		
		if(editbean instanceof EditableReportInsertDataBean){
			checkForm(rrequest, pid, null, name, code);
		}else if(editbean instanceof EditableReportUpdateDataBean){
			Long id = Long.valueOf(mRowData.get(Messages.getString("SystemInfoIntercepter.3"))); //$NON-NLS-1$
			checkForm(rrequest, pid, id, name, code);
		}
		
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}
	
	/**
	 * 检查重复性
	 * @return
	 */
	public void checkForm(ReportRequest rrequest,Long pid,Long id,String name,String code){
		SystemInfoService service = AppConfig.getInstance().getWac().getBean(SystemInfoService.class);
		SystemInfo systemInfo = null;
		
		if(id == null || id == 0L){		//插入
			check(rrequest, name, code, pid);
		}else{		//修改
			systemInfo = service.get(id);
			if(systemInfo.getName().equals(name) || systemInfo.getCode().equals(code)){
				if(systemInfo.getName().equals(name)){
					check(rrequest, null, code, pid);
				}else{
					check(rrequest, name, null, pid);
				}
			}else{
				check(rrequest, name, code, pid);
			}
		}
	}

	
	public void check(ReportRequest rrequest ,String name,String code,Long pid){
		SystemInfoService service = AppConfig.getInstance().getWac().getBean(SystemInfoService.class);
		List<SystemInfo> list = null;
		
		list = service.findByCode(code);
		if(list != null && list.size() != 0){	//是否有此代码的数据
			for(SystemInfo s : list){
				if(pid.equals(s.getParameter().getId())){		//是否是此标识号下的
					rrequest.getWResponse().getMessageCollector().warn(Messages.getString("SystemInfoIntercepter.4"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
					break;
				}
			}
		}
		
		list = service.findByName(name);
		for(SystemInfo s : list){
			if(pid.equals(s.getParameter().getId())){
				rrequest.getWResponse().getMessageCollector().warn(Messages.getString("SystemInfoIntercepter.5"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
				break;
			}
		}

	}
	
}

